package net.aiion.weave.impl.painters.stream.text;

import java.io.PrintStream;

import net.aiion.weave.impl.painters.PaintBrushMessage;
import net.aiion.weave.impl.painters.stream.AbstractStreamBrush;
import net.aiion.weave.spi.messaging.annotations.Receives;
import net.aiion.weave.spi.naming.annotations.Bound;
import net.aiion.weave.spi.naming.annotations.Named;
import net.aiion.weave.spi.naming.annotations.NotScoped;
import net.aiion.weave.spi.painters.stream.text.TextPaintBrush;
import net.aiion.weave.spi.routing.Route;

@Named
@NotScoped
public class TextPaintBrushImpl extends AbstractStreamBrush implements TextPaintBrush
{
    private static final String TEXT_EXTENSION = "txt";

    private PrintStream printStream;

    public TextPaintBrushImpl() {
    }

    @Bound
    public void initializePrintStream() {
        printStream = new PrintStream(getOutputStream());
    }

    @Receives("net.aiion.weave.painters")
    public Object resolvePaintBrush(final PaintBrushMessage aPaintBrushMessage) {
        final Route route = aPaintBrushMessage.getRoute();
        if (TEXT_EXTENSION.equals(route.getExtension())) {
            return this;
        }

        return null;
    }

    @Override
    public void write(final String aString) {
        printStream.print(aString);
    }

    @Override
    public void writeLine(final String aString) {
        printStream.println(aString);
    }

    @Override
    public void closeBrush() {
        printStream.close();
    }
}
